<template>
  <div class="AdminNewsCreate">
    <h1>AdminNews Create</h1>
    <el-form
      ref="ruleForm"
      :model="ruleForm"
      :rules="rules"
      label-width="120px"
      class="demo-ruleForm"
    >
      <el-form-item label="标题" prop="title">
        <el-input v-model="ruleForm.title"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="desc">
        <el-input v-model="ruleForm.desc"></el-input>
      </el-form-item>
      <el-form-item label="详情" prop="detail">
        <el-input v-model="ruleForm.detail" type="textarea"></el-input>
      </el-form-item>
      <el-form-item label="创建时间" required>
        <el-col :span="12">
          <el-form-item prop="create_at">
            <el-date-picker
              v-model="ruleForm.create_at"
              type="date"
              placeholder="Pick a date"
              style="width: 100%"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </el-form-item>

      <el-form-item label="发布者" prop="region">
        <el-select v-model="ruleForm.author" placeholder="请选择发布者">
          <el-option label="新华社" value="新华社"></el-option>
          <el-option label="人民日报" value="人民日报"></el-option>
          <el-option label="央视网" value="央视网"></el-option>
          <el-option label="张三频道" value="张三频道"></el-option>
          <el-option label="凤凰新闻" value="凤凰新闻"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >Create</el-button
        >
        <el-button @click="resetForm('ruleForm')">Reset</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "AdminNewsCreate",
  data() {
    return {
      ruleForm: {
        title: "",
        desc: "",
        detail: "",
        create_at: "",
        author: "",
      },
      rules: {
        title: [
          {
            required: true,
            message: "请输入标题",
            trigger: "blur",
          },
          {
            min: 5,
            max: 30,
            message: "长度在 5 ~ 30 之间",
            trigger: "change",
          },
        ],
        create_at: [
          {
            type: "date",
            required: true,
            message: "Please pick a date",
            trigger: "change",
          },
        ],
        desc: [
          {
            required: true,
            message: "请输入描述",
            trigger: "blur",
          },
        ],
        detail: [
          {
            required: true,
            message: "请输入详情",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          try {
            await this.$store.dispatch("news/createNewsAction", this.ruleForm);
            this.$router.push({
              name: "AdminNews",
            });
          } catch (e) {
            this.$message.error("网络错误");
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped></style>
